import _ from 'lodash';
import assert from 'assert';
import Router from 'koa-router';
import tokenAuth from '../middleware/tokenAuth';
import messageHandler from '../handler/messageHandler';

const router = new Router({
  prefix: '/api/messages',
});

router.use(tokenAuth());

router.post('/post', async (ctx) => {
  const input = ctx.request.body;
  const { channel, body } = input;
  assert(channel, 'no_channel');
  assert(typeof body === 'string' && body !== '', 'empty_message');

  messageHandler.post(input.channel, ctx.viewer, input);

  ctx.body = {
    ok: true,
  };
});

router.post('/delete', (ctx) => {
  ctx.body = {
    message: 'delete a message',
    viewer: _.pick(ctx.viewer, ['username', 'mobile']),
  };
});

router.post('/update', (ctx) => {
  ctx.body = {
    message: 'update a message',
  };
});

export default router;
